home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 3D / SetupGL / SetupGL Main Windowed.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  18.2 KB  |  599 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        SetupGL Main Windowed.c
  3.  
  4.     Contains:    An example of the use of the SeupGL utility code for windowed applications.
  5.  
  6.     Written by:    Geoff Stahl
  7.  
  8.     Copyright:    2000 Apple Computer, Inc., All Rights Reserved
  9.  
  10.     Change History (most recent first):
  11.  
  12.  
  13.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  14.                 ("Apple") in consideration of your agreement to the following terms, and your
  15.                 use, installation, modification or redistribution of this Apple software
  16.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  17.                 please do not use, install, modify or redistribute this Apple software.
  18.  
  19.                 In consideration of your agreement to abide by the following terms, and subject
  20.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  21.                 copyrights in this original Apple software (the "Apple Software"), to use,
  22.                 reproduce, modify and redistribute the Apple Software, with or without
  23.                 modifications, in source and/or binary forms; provided that if you redistribute
  24.                 the Apple Software in its entirety and without modifications, you must retain
  25.                 this notice and the following text and disclaimers in all such redistributions of
  26.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  27.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  28.                 Apple Software without specific prior written permission from Apple.  Except as
  29.                 expressly stated in this notice, no other rights or licenses, express or implied,
  30.                 are granted by Apple herein, including but not limited to any patent rights that
  31.                 may be infringed by your derivative works or by other works in which the Apple
  32.                 Software may be incorporated.
  33.  
  34.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  35.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  36.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  37.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  38.                 COMBINATION WITH YOUR PRODUCTS.
  39.  
  40.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  41.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  42.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  44.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  45.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  46.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47.     
  48.          <1>     3/22/00    ggs     New windowed demo code
  49.         <6+>     2/24/00    ggs        Windowed mode support 
  50.          <6>     1/26/00    ggs     Add preflight support, handle failure conditions throughout app
  51.                                     better
  52.          <5>     1/24/00    ggs     fixed Apple menu selection, fixed aglfont texture row length
  53.                                     problem, fixed suspend/resume, fixed update
  54.          <4>    12/18/99    ggs     Fix headers
  55.          <3>    12/18/99    ggs     Added window/fullscreen toggle support and error window
  56.          <2>    11/28/99    ggs     Standard toolbox application
  57.          <1>    11/28/99    ggs     Initial Add
  58.          <6>     9/14/99    GGS        Corrected buffer rect handling and cleaned up code
  59.          <5>     7/14/99    GGS        Fixed multi-monitor window centering
  60.          <4>     7/13/99    GGS        Add work around for over zealous checking in single buffer DSp context attributes
  61.          <3>     7/5/99     GGS        Now correctly handle multi-monitor (DSp front buffer for single device; Window on top of context for multiple devices)
  62.          <2>     5/28/99    GGS     Added better multi-monitor support, clean code, corrected blanking bug, added timing, correct pixel formats
  63.          <1>        ?        ?      Initial build
  64. */
  65.  
  66.  
  67. // system includes ----------------------------------------------------------
  68.  
  69. #include <Devices.h>
  70. #include <Dialogs.h>
  71. #include <DriverServices.h>
  72. #include <Events.h>
  73. #include <LowMem.h>
  74. #include <TextEdit.h>
  75. #include <ToolUtils.h>
  76. #include <Windows.h>
  77.  
  78. #include <math.h>
  79. #include <stdio.h>
  80. #include <string.h>
  81.  
  82. #include <gl.h>
  83.  
  84. // project includes ---------------------------------------------------------
  85.  
  86. #include "SetupGL.h"
  87.  
  88. // prototypes ---------------------------------------------------------------
  89.  
  90. void InitToolbox(void);
  91. Boolean SetUp (void);
  92. void DoMenu (SInt32 menuResult);
  93. void DoKey (SInt8 theKey, SInt8 theCode);
  94. void DoUpdate (WindowPtr pWindow);
  95. void DoEvent (void);
  96. void CleanUp (void);
  97.  
  98. void drawGL(WindowPtr pWindow, AGLContext aglContext, GLuint fontList);
  99.  
  100. void DrawFrameRate (GLuint fontList);
  101. void DrawContextInfo (GLuint fontList);
  102.  
  103. void DrawPStringGL (Str255 pstrOut, GLuint fontList);
  104. void DrawCStringGL (char * cstrOut, GLuint fontList);
  105. GLuint BuildFontGL (AGLContext ctx, GLint fontID, Style face, GLint size);
  106. void DeleteFontGL (GLuint fontList);
  107.  
  108.  
  109. // statics/globals (internal only) ------------------------------------------
  110.  
  111. // Menu defs
  112. enum 
  113. {
  114.     kNumRes = 10,
  115.     kNumFreqs = 11,
  116.  
  117.     kMenuApple = 128,
  118.     kMenuFile = 129,
  119.     
  120.     kAppleAbout = 1,
  121.     kFileQuit = 1
  122. };
  123.  
  124. enum 
  125. {
  126.     kForegroundSleep = 0,
  127.     kBackgroundSleep = 10
  128. };
  129.  
  130. const RGBColor    rgbWhite    = { 0xFFFF, 0xFFFF, 0xFFFF };
  131.  
  132. SInt32 gSleepTime = kForegroundSleep;
  133. Boolean gDone = false, gfFrontProcess = true;
  134.  
  135. char gstrContext1 [256] = ""; // mode strings
  136. char gstrContext2 [256] = "";
  137.  
  138. structGLWindowInfo glInfo1, glInfo2;
  139. AGLContext aglContext1 = 0, aglContext2 = 0;
  140. GLuint gFontList1 = 0, gFontList2 = 0;
  141.  
  142. WindowPtr gpWindow1 = NULL, gpWindow2 = NULL;
  143.  
  144.  
  145. // functions (internal/private) ---------------------------------------------
  146.  
  147. void InitToolbox(void)
  148. {
  149.     MenuHandle menu;
  150.  
  151.     MaxApplZone ();
  152.  
  153.     InitGraf((Ptr) &qd.thePort);
  154.     InitFonts();
  155.     InitWindows();
  156.     InitMenus();
  157.     TEInit();
  158.     InitDialogs(nil);
  159.     InitCursor();
  160.     
  161.     qd.randSeed =  TickCount();
  162.  
  163.     // Init Menus
  164.     menu = NewMenu (kMenuApple, "\p\024");            // new  apple menu
  165.     InsertMenu (menu, 0);                            // add menu to end
  166.     AppendResMenu(menu, 'DRVR');
  167.     
  168.     menu = NewMenu (kMenuFile, "\pFile");            // new menu
  169.     InsertMenu (menu, 0);                            // add menu to end
  170.     AppendMenu (menu, "\pQuit/Q");                     // add items
  171.     
  172.     DrawMenuBar();
  173.  
  174. // --------------------------------------------------------------------------
  175.  
  176. Boolean SetUp (void)
  177. {
  178.     short i;
  179.     short fNum;
  180.  
  181.     InitToolbox ();
  182.     
  183.     if (PreflightGL (false))
  184.     {
  185.         Rect rectWin1 = {100, 100, 500, 500},  rectWin2 = {300, 350, 600, 650};
  186.         
  187.         memset(&glInfo1, 0, sizeof glInfo1);
  188.         glInfo1.fAcceleratedMust = true;     // must renderer be accelerated?
  189.         glInfo1.VRAM = 0 * 1048576;            // minimum VRAM (if not zero this is always required)
  190.         glInfo1.textureRAM = 0 * 1048576;    // minimum texture RAM (if not zero this is always required)
  191.         glInfo1.fDraggable = false;         // desired vertical refresh frquency in Hz (0 = any)
  192.         glInfo1.fmt = 0;                    // output pixel format
  193.         
  194.         i = 0;
  195.         glInfo1.aglAttributes [i++] = AGL_RGBA;
  196.         glInfo1.aglAttributes [i++] = AGL_DOUBLEBUFFER;
  197.         glInfo1.aglAttributes [i++] = AGL_ACCELERATED;
  198.         glInfo1.aglAttributes [i++] = AGL_DEPTH_SIZE;
  199.         glInfo1.aglAttributes [i++] = 16;
  200.         glInfo1.aglAttributes [i++] = AGL_NONE;
  201.         gpWindow1 = (WindowPtr) NewCWindow (NULL, &rectWin1, "\pWindow 1", true, kWindowPlainDialogProc, (WindowPtr)-1, 0, 0);
  202.         BuildGLFromWindow ((CGrafPtr) gpWindow1, &aglContext1, &glInfo1);
  203.         if (!aglContext1)
  204.         {
  205.             DestroyGLFromWindow (&aglContext1, &glInfo1);
  206.             sprintf (gstrContext1, "No context");            
  207.         }
  208.         else// set mode string
  209.             sprintf (gstrContext1, "%d x %d", gpWindow1->portRect.right - gpWindow1->portRect.left, gpWindow1->portRect.bottom - gpWindow1->portRect.top);            
  210.             
  211.         memset(&glInfo2, 0, sizeof glInfo2);
  212.         glInfo2.fAcceleratedMust = true;     // must renderer be accelerated?
  213.         glInfo2.VRAM = 0 * 1048576;            // minimum VRAM (if not zero this is always required)
  214.         glInfo2.textureRAM = 0 * 1048576;    // minimum texture RAM (if not zero this is always required)
  215.         glInfo2.fDraggable = true;             // desired vertical refresh frquency in Hz (0 = any)
  216.         glInfo2.fmt = 0;                    // output pixel format
  217.         
  218.         i = 0;
  219.         glInfo2.aglAttributes [i++] = AGL_RGBA;
  220.         glInfo2.aglAttributes [i++] = AGL_DOUBLEBUFFER;
  221. //        glInfo2.aglAttributes [i++] = AGL_ACCELERATED; // can't use this for dragable windows
  222.         glInfo2.aglAttributes [i++] = AGL_DEPTH_SIZE;
  223.         glInfo2.aglAttributes [i++] = 16;
  224.         glInfo2.aglAttributes [i++] = AGL_NONE;
  225.         gpWindow2 = (WindowPtr) NewCWindow (NULL, &rectWin2, "\pWindow 2", true, kWindowFloatGrowProc, (WindowPtr)-1, 0, 0);
  226.         BuildGLFromWindow ((CGrafPtr) gpWindow2, &aglContext2, &glInfo2);
  227.         if (!aglContext2)
  228.         {
  229.             DestroyGLFromWindow (&aglContext2, &glInfo2);
  230.             sprintf (gstrContext2, "No context");            
  231.         }
  232.         else// set mode string
  233.             sprintf (gstrContext2, "%d x %d", gpWindow2->portRect.right - gpWindow2->portRect.left, gpWindow2->portRect.bottom - gpWindow2->portRect.top);            
  234.     }
  235.         
  236.     GetFNum("\pMonaco", &fNum);                                    // build font
  237.     if (aglContext1)
  238.         gFontList1 = BuildFontGL (aglContext1, fNum, normal, 9);
  239.     GetFNum("\pGeneva", &fNum);                                    // build font
  240.     if (aglContext2)
  241.         gFontList2 = BuildFontGL (aglContext2, fNum, italic, 9);
  242.     return true;
  243. }
  244.  
  245. // --------------------------------------------------------------------------
  246.  
  247. void DoMenu (SInt32 menuResult)
  248. {
  249.     SInt16 theMenu;
  250.     SInt16 theItem;
  251.     Str255 daName;
  252.     MenuRef theMenuHandle;
  253.         
  254.     theMenu = HiWord(menuResult);
  255.     theItem = LoWord(menuResult);
  256.     theMenuHandle = GetMenuHandle(theMenu);
  257.  
  258.     switch (theMenu)
  259.     {
  260.         case kMenuApple:
  261.             switch (theItem)
  262.             {
  263.                 case kAppleAbout:
  264.                     break;
  265.                 default:
  266.                     GetMenuItemText (theMenuHandle, theItem, daName);
  267.                     OpenDeskAcc(daName);
  268.                     break;
  269.             }
  270.             break;
  271.         case kMenuFile:
  272.             switch (theItem)
  273.             {
  274.                 case kFileQuit:
  275.                     gDone = true;
  276.                     break;
  277.             }
  278.             break;
  279.     }
  280.     HiliteMenu(0);
  281.     DrawMenuBar();
  282. }
  283.  
  284. // --------------------------------------------------------------------------
  285.  
  286. void DoKey (SInt8 theKey, SInt8 theCode)
  287. {
  288.     #pragma unused (theCode, theKey)
  289.     // do nothing
  290. }
  291.  
  292. // --------------------------------------------------------------------------
  293.  
  294. void DoUpdate (WindowPtr pWindow)
  295. {
  296.     if ((pWindow == gpWindow1) && aglContext1)
  297.     {
  298.         ResumeGL ((CGrafPtr) gpWindow1, aglContext1);
  299.         drawGL (gpWindow1, aglContext1, gFontList1);
  300.     }
  301.     if ((pWindow == gpWindow2) && aglContext2)
  302.     {
  303.         ResumeGL ((CGrafPtr) gpWindow2, aglContext2);
  304.         drawGL (gpWindow1, aglContext2, gFontList2);
  305.     }
  306. }
  307.  
  308. // --------------------------------------------------------------------------
  309.  
  310. void DoEvent (void)
  311. {
  312.     EventRecord theEvent;
  313.     Rect rectGrow;
  314.     SInt32 menuResult;
  315.     WindowRef whichWindow;
  316.     GrafPtr pGrafSave;
  317.     long grow;
  318.     SInt16 whatPart;
  319.     SInt8 theKey;
  320.     SInt8 theCode;
  321.     Boolean fProcessed = false;
  322.     
  323.     if (WaitNextEvent(everyEvent, &theEvent, gSleepTime, NULL))
  324.     {
  325.         {
  326.             switch (theEvent.what)
  327.             {
  328.                 case mouseDown:
  329.                     whatPart = FindWindow(theEvent.where, &whichWindow);
  330.                     SelectWindow (whichWindow);
  331.                     switch (whatPart)
  332.                     {
  333.                         case inGoAway:
  334.                             break;
  335.                         case inMenuBar:
  336.                             DrawMenuBar();
  337.                             menuResult = MenuSelect(theEvent.where);
  338.                             if (HiWord(menuResult) != 0)
  339.                                 DoMenu(menuResult);
  340.                             break;
  341.                         case inDrag:
  342.                             if (gpWindow2 == whichWindow)
  343.                                 DragWindow (whichWindow, theEvent.where, &(**LMGetGrayRgn()).rgnBBox);
  344.                                 // must reset the drawable just incase we moved renderers
  345.                                 aglSetDrawable(aglContext2, (CGrafPtr) NULL);
  346.                                 aglSetDrawable(aglContext2, (CGrafPtr) gpWindow2);
  347.                             break;
  348.                         case inGrow:
  349.                             SetRect (&rectGrow, 100, 100, 20000, 20000);
  350.                             grow = GrowWindow (whichWindow, theEvent.where, &rectGrow);
  351.                             if (grow)
  352.                             {
  353.                                 SizeWindow (whichWindow, grow & 0x0000FFFF, grow >> 16, true);
  354.                                 // do content stuff here
  355.                                 SetPort (whichWindow);
  356.                                 InvalRect (&whichWindow->portRect);                // redraw all
  357.                                 if ((whichWindow == gpWindow2) && aglContext2)
  358.                                 {
  359.                                     aglSetDrawable(aglContext2, (CGrafPtr) NULL);
  360.                                     aglSetDrawable(aglContext2, (CGrafPtr) gpWindow2);
  361.                                     sprintf (gstrContext2, "%d x %d", gpWindow2->portRect.right - gpWindow2->portRect.left, gpWindow2->portRect.bottom - gpWindow2->portRect.top);            
  362.                                 }
  363.                                 if ((whichWindow == gpWindow1) && aglContext1)
  364.                                 {
  365.                                     aglSetDrawable(aglContext1, (CGrafPtr) NULL);
  366.                                     aglSetDrawable(aglContext1, (CGrafPtr) gpWindow1);
  367.                                     sprintf (gstrContext1, "%d x %d", gpWindow1->portRect.right - gpWindow1->portRect.left, gpWindow1->portRect.bottom - gpWindow1->portRect.top);            
  368.                                 }
  369.                                 
  370.                             }
  371.                             break;
  372.                         case inSysWindow:
  373.                             SystemClick(&theEvent, whichWindow);
  374.                             break;
  375.                     }
  376.                     break;
  377.                 case keyDown:
  378.                 case autoKey:
  379.                     theKey = theEvent.message & charCodeMask;
  380.                     theCode = (theEvent.message & keyCodeMask) >> 8;
  381.                     if ((theEvent.modifiers & cmdKey) != 0)
  382.                     {
  383.                         menuResult = MenuKey(theKey);
  384.                         if (HiWord(menuResult) != 0)
  385.                             DoMenu (menuResult);
  386.                     }
  387.                     else
  388.                         DoKey (theKey, theCode);
  389.                     break;
  390.                 case updateEvt:
  391.                     whichWindow = (WindowRef) theEvent.message;
  392.                     GetPort (&pGrafSave);
  393.                     SetPort ((GrafPtr) whichWindow);
  394.                     BeginUpdate(whichWindow);
  395.                     DoUpdate(whichWindow);
  396.                     SetPort ((GrafPtr) whichWindow);
  397.                     EndUpdate(whichWindow);
  398.                     SetPort (pGrafSave);
  399.                     break;
  400.                 case diskEvt:
  401.                     break;
  402.                 case osEvt:
  403.                     if (theEvent.message & 0x01000000)        //    Suspend/resume event
  404.                     {
  405.                         if (theEvent.message & 0x00000001)    //    Resume
  406.                         {
  407.                             
  408.                             gSleepTime = kForegroundSleep;
  409.                             if (gpWindow1 && aglContext1)
  410.                                 DoUpdate (gpWindow1);
  411.                             if (gpWindow2 && aglContext2)
  412.                                 DoUpdate (gpWindow2);
  413.                             gfFrontProcess = true;
  414.                         }
  415.                         else
  416.                         {
  417.                             gSleepTime = kBackgroundSleep;    //    Suspend
  418.                             gfFrontProcess = false;
  419.                         }
  420.                     }
  421.                     break;
  422.  
  423.                 case kHighLevelEvent:
  424.                     AEProcessAppleEvent(&theEvent);
  425.                     break;
  426.             }
  427.         }
  428.     }
  429.     else
  430.     {
  431.         DoUpdate (gpWindow1);
  432.         DoUpdate (gpWindow2);
  433.     }
  434. }
  435.  
  436. // --------------------------------------------------------------------------
  437.  
  438. void CleanUp (void)
  439. {
  440.     MenuHandle hMenu;
  441.  
  442.     DeleteFontGL (gFontList1);
  443.     DeleteFontGL (gFontList2);
  444.     DestroyGLFromWindow (&aglContext1, &glInfo1);
  445.     DestroyGLFromWindow (&aglContext2, &glInfo2);
  446.  
  447.     if (gpWindow1)
  448.     {
  449.         DisposeWindow ((WindowPtr)gpWindow1);
  450.         gpWindow1 = NULL;
  451.     }
  452.     if (gpWindow2)
  453.     {
  454.         DisposeWindow ((WindowPtr)gpWindow2);
  455.         gpWindow2 = NULL;
  456.     }
  457.  
  458.     hMenu = GetMenuHandle (kMenuFile);
  459.     DeleteMenu (kMenuFile);
  460.     DisposeMenu (hMenu);
  461.  
  462.     hMenu = GetMenuHandle (kMenuApple);
  463.     DeleteMenu (kMenuApple);
  464.     DisposeMenu (hMenu);
  465. }
  466.  
  467. // --------------------------------------------------------------------------
  468.  
  469. int main (void)
  470. {
  471.     if (SetUp ())    
  472.         while (!gDone) 
  473.             DoEvent ();
  474.     CleanUp ();
  475.     return 0;
  476. }
  477.  
  478.  
  479. #pragma mark -
  480. //-----------------------------------------------------------------------------------------------------------------------
  481.  
  482. // OpenGL Drawing
  483.  
  484. void drawGL(WindowPtr pWindow, AGLContext aglContext, GLuint fontList)
  485. {
  486.     static float f, s, c;
  487.     GLboolean fState = GL_FALSE;
  488.  
  489.     f += 0.01;
  490.     s = sin(f);
  491.     c = cos(f);
  492.  
  493.     glClearColor(0.15f, 0.15f, 0.15f, 1.0f);                    // Clear color buffer to dark grey
  494.     glClear(GL_COLOR_BUFFER_BIT);
  495.     
  496.     glBegin(GL_POLYGON);                                        // Draw a smooth shaded polygon
  497.     glColor3d(1.0, 0.0, 0.0);
  498.     glVertex3d(s, c, 0.0);
  499.     glColor3d(0.0, 1.0, 0.0);
  500.     glVertex3d(-c, s, 0.0);
  501.     glColor3d(0.0, 0.0, 1.0);
  502.     glVertex3d(-s, -c, 0.0);
  503.     glColor3d(0.7, 0.7, 0.7);
  504.     glVertex3d(c, -s, 0.0);
  505.     glEnd();
  506.  
  507.     // Draw frame rate (set color and position first)
  508.     glColor3d(1.0, 1.0, 1.0);
  509.     glRasterPos3d (-0.95, ((pWindow->portRect.bottom - pWindow->portRect.top) - 40.0) / (float) (pWindow->portRect.bottom - pWindow->portRect.top), 0.0); 
  510.     DrawFrameRate (fontList);
  511.     glRasterPos3d (-0.95, ((pWindow->portRect.bottom - pWindow->portRect.top) - 65.0) / (float) (pWindow->portRect.bottom - pWindow->portRect.top), 0.0); 
  512.     if (aglContext == aglContext1)
  513.         DrawCStringGL (gstrContext1, fontList);
  514.     else
  515.         DrawCStringGL (gstrContext2, fontList);
  516.     
  517.  
  518.     glRasterPos3d (-0.95, -((pWindow->portRect.bottom - pWindow->portRect.top) - 45.0) / (float) (pWindow->portRect.bottom - pWindow->portRect.top), 0.0); 
  519.     DrawCStringGL ((char*) glGetString (GL_VENDOR), fontList);
  520.     glRasterPos3d (-0.95, -((pWindow->portRect.bottom - pWindow->portRect.top) - 20.0) / (float) (pWindow->portRect.bottom - pWindow->portRect.top), 0.0); 
  521.     DrawCStringGL ((char*) glGetString (GL_RENDERER), fontList);
  522.  
  523.     aglSwapBuffers(aglContext);                                        // send swap command
  524. }
  525.  
  526. //-----------------------------------------------------------------------------------------------------------------------
  527.  
  528. // Draw frame rate in curent color at current raster positon with provided font display list
  529.  
  530. void DrawFrameRate (GLuint fontList)
  531. {    
  532.     static char aChar[256] = "";
  533.     static AbsoluteTime time = {0,0};
  534.     static long frames = 0;
  535.  
  536.     AbsoluteTime currTime = UpTime ();
  537.     float deltaTime = (float) AbsoluteDeltaToDuration (currTime, time);
  538.     
  539.     frames++;
  540.  
  541.     if (0 > deltaTime)    // if negative microseconds
  542.         deltaTime /= -1000000.0;
  543.     else                // else milliseconds
  544.         deltaTime /= 1000.0;
  545.     if (0.5 <= deltaTime)    // has update interval passed
  546.     {
  547.         sprintf (aChar, "Swaps/Sec: %0.1f", frames / deltaTime);
  548.         time = currTime;    // reset for next time interval
  549.         frames = 0;
  550.     }
  551.     
  552.     DrawCStringGL (aChar, fontList);
  553. }
  554.  
  555. #pragma mark -
  556. //-----------------------------------------------------------------------------------------------------------------------
  557.  
  558. void DrawPStringGL (Str255 pstrOut, GLuint fontList)
  559. {
  560.     GLint i;
  561.     for (i = 1; i <= pstrOut[0]; i++)
  562.         glCallList (fontList + pstrOut[i]);
  563. }
  564.  
  565. //-----------------------------------------------------------------------------------------------------------------------
  566.  
  567. void DrawCStringGL (char * cstrOut, GLuint fontList)
  568. {
  569.     GLint i = 0;
  570.     while (cstrOut [i])
  571.         glCallList (fontList + cstrOut[i++]);
  572. }
  573.  
  574. //-----------------------------------------------------------------------------------------------------------------------
  575.  
  576. GLuint BuildFontGL (AGLContext ctx, GLint fontID, Style face, GLint size)
  577. {
  578.     GLuint listBase = glGenLists (256);
  579.     if (aglUseFont (ctx, fontID , face, size, 0, 256, (long) listBase))
  580.     {
  581.         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  582.         return listBase;
  583.     }
  584.     else
  585.     {
  586.         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  587.         glDeleteLists (listBase, 256);
  588.         return 0;
  589.     }
  590. }
  591.  
  592. //-----------------------------------------------------------------------------------------------------------------------
  593.  
  594. void DeleteFontGL (GLuint fontList)
  595. {
  596.     if (fontList)
  597.         glDeleteLists (fontList, 256);
  598. }